<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    <title>测试接口跨域问题</title>
    <style>
        /* *{
            display: flex;
            justify-content: center;
            align-items: center;
        } */
    </style>
</head>
<body>
    <button id="btnGET">GET</button>
    <button id="btnPOST">POST</button>
    <button id="btnDelete">DELETE</button>
    <button id="btnJSONP">JSONP</button>
</body>
</html>
<script>
    $(function(){
        // 1.测试 GET 接口
        $('#btnGET').on('click',function(){
            $.ajax({
                type: 'GET',
                url:'http://127.0.0.1/api/get',
                data:{name: 'zs', age: 20},
                success: (res)=>{
                    console.log(res);
                }
            })
        });
        // 1.测试 POST 接口。需要配置请求头
        $('#btnPOST').on('click',function(){
            $.ajax({
                type: 'POST',
                url:'http://127.0.0.1/api/post',
                data:{bookname: '水浒传', author: '施耐庵'},
                success: (res)=>{
                    console.log(res);
                }
            })
        })
	    // 3.为删除按钮绑定点击事件
        $('#btnDelete').on('click', function(){
            $.ajax({
                type:'DELETE',
                url:'http://127.0.0.1/api/delete',
                success: function(res){
                    console.log(res);
                }
            })
        })
        // 4. 为 jsonp 按钮绑定点击事件处理函数
        $('#btnJSONP').on('click', function(){
            $.ajax({
                type:'get',
                url:'http://127.0.0.1/api/jsonp',
                dataType: 'jsonp',
                success: function(res){
                    console.log(res);
                }
            })
        })
    })
</script>
